package cn.cmkj.auction.ui

import android.graphics.PixelFormat
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import android.widget.ImageButton
import android.widget.TextView
import android.widget.Toast
import cn.cmkj.auction.R
import cn.cmkj.auction.app.BaseActivity
import cn.cmkj.auction.app.Urls
import cn.cmkj.auction.utils.HttpCallback
import cn.cmkj.auction.widget.X5WebView
import com.tencent.smtt.sdk.WebSettings
import com.tencent.smtt.sdk.WebView
import com.tencent.smtt.sdk.WebViewClient
import org.json.JSONException
import org.json.JSONObject
import org.xutils.common.Callback
import org.xutils.common.util.LogUtil
import org.xutils.http.HttpMethod
import org.xutils.http.RequestParams
import org.xutils.view.annotation.ContentView
import org.xutils.x

/**
 * Created by cunguoyao on 2018/3/22.
 */
@ContentView(R.layout.activity_webview)
class AboutUsSub : BaseActivity(), View.OnClickListener {

    private var sub = 1
    private val defaultTitle = "关于我们"

    private val mimeType = "text/html; charset=UTF-8"
    private lateinit var titleBack : ImageButton
    private lateinit var titleText : TextView
    private lateinit var webview : X5WebView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        x.view().inject(this)
        window.setFormat(PixelFormat.TRANSLUCENT)
        sub = this.intent.getIntExtra("sub", 1)
        titleBack = findViewById(R.id.title_back) as ImageButton
        titleText = findViewById(R.id.title_text) as TextView
        webview = findViewById(R.id.webview) as X5WebView
        webview.isHorizontalScrollBarEnabled = false
        webview.isVerticalScrollBarEnabled = false
        val webSettings = webview.settings
        webSettings.defaultTextEncodingName = "UTF-8"
        webSettings.javaScriptEnabled = true
        webSettings.domStorageEnabled = true
        webSettings.databaseEnabled = true
        webSettings.useWideViewPort = true
        webSettings.supportZoom()
        webSettings.builtInZoomControls = true
        webSettings.displayZoomControls = true
        webSettings.layoutAlgorithm = WebSettings.LayoutAlgorithm.SINGLE_COLUMN
        webSettings.loadWithOverviewMode = true
        webview.webViewClient = object : WebViewClient() {
            override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
                view!!.loadUrl(url)
                return true
            }
        }
        titleText.text = defaultTitle
        titleBack.setOnClickListener(this)
        fetchData()
    }

    fun fetchData() {
        val params = RequestParams(Urls.api)
        params.addParameter("method", Urls.aboutUs)
        params.addParameter("article_id", sub)
        x.http().request(HttpMethod.POST, params, object : HttpCallback(this) {
            override fun success(s: String) {
                var jsonObject : JSONObject? = null
                try {
                    jsonObject = JSONObject(s)
                    val jsonArray = jsonObject.optJSONArray("data")
                    var json : JSONObject = jsonArray.get(0) as JSONObject
                    val title = json.optString("title", defaultTitle)
                    val content = json.optString("content", "")
                    titleText.text = title
                    webview.loadData(content, mimeType, null)
                } catch (e: JSONException) {
                    e.printStackTrace()
                }

            }

            override fun error(ex: Throwable?, msg: String, isOnCallback: Boolean) {
                ex?.printStackTrace()
                LogUtil.e("---onError-----" + "onError")
                Toast.makeText(this@AboutUsSub, if (TextUtils.isEmpty(msg)) "请求失败" else msg, Toast.LENGTH_SHORT).show()
            }

            override fun cancel(cex: Callback.CancelledException) {
                LogUtil.e("---onCancelled-----" + "onCancelled")
            }
        })
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.title_back ->
                finish()
        }
    }
}